package com.gu.service.impl;

import com.gu.VO.ServerResponse;
import com.gu.dao.ApartmentinfoDAO;
import com.gu.entity.ApartmentinfoDO;
import com.gu.service.ApartmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.service.impl
 * @ClassName: ApartmentInfoServiceImpl
 * @Author: gusongliao
 * @Description: 小区简介服务实现
 * @createDate 2021/03/23 21:36
 */

@Service
public class ApartmentInfoServiceImpl implements ApartmentInfoService {

    @Autowired
   private ApartmentinfoDAO apartmentinfoDAO;


    @Override
    public ServerResponse update(ApartmentinfoDO entity) {
        entity.setId(1);
        Integer searchResult =  apartmentinfoDAO.update(entity);

        if (searchResult != 1){
            return ServerResponse.error(9,"更新小区简介失败");
        }
        return ServerResponse.SUCCESS;
    }

    @Override
    public ApartmentinfoDO getById(Integer id) {
        return apartmentinfoDAO.getById(id);
    }

    @Override
    public Boolean isFirstLogin() {
        ApartmentinfoDO apartmentinfoDO = apartmentinfoDAO.getById(1);
        if (apartmentinfoDO != null) {
            if (apartmentinfoDO.getPassword() == null || apartmentinfoDO.getPassword().equals("") || apartmentinfoDO.getPassword().trim().equals("")) {
                return true;
            }
        }
        return false;
    }

    @Override
    public ServerResponse updatePassword(String password,String rePassword) {
        if (password != null && rePassword != null) {
            if (password.equals(rePassword)) {
                if (password.trim().equals("")) {
                    return ServerResponse.error(28, "请输入除空格之外的密码");
                }
                int result = apartmentinfoDAO.updatePassword(password);
                if (result == 1) {
                    return ServerResponse.SUCCESS;
                }
            } else {
                return ServerResponse.error(27, "两次输入密码不相同，请重新输入");
            }
        }
            return ServerResponse.error(26, "设置密码失败");

    }

    @Override
    public ServerResponse loginSystem(String password) {
        ApartmentinfoDO apartmentinfoDO = apartmentinfoDAO.getById(1);
        if (apartmentinfoDO != null && apartmentinfoDO.getPassword().equals(password)){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(29,"密码输入不正确，请重新输入");
    }
}
